#include "revolutionObject.h"
#include <cmath>



/***************************************/
/***** Constructeur / Destructeur ******/
/***************************************/

RevolutionObject::RevolutionObject(int nb_pas, double angle) : Objet3D(), _nb_pas(nb_pas)
{
_tex = NULL;
_generatePoints(angle);
}

RevolutionObject::~RevolutionObject() 
{}


/**********************************/
/* Fonctions de calcul des points */
/**********************************/

list<Vertex> RevolutionObject::_generateProfil()
{
	list<Vertex> pouet ;
	return pouet;
}


void RevolutionObject::_generatePoints(double angle)
{
list<Vertex> profil = _generateProfil();
_nb_vertex = profil.size();


	int i =0;
	double pas_angulaire = (_nb_pas != 0) ? angle/_nb_pas:angle;
	double xtmp,ytmp,ztmp;
	
	_points.insert(_points.end(), profil.begin(), profil.end());
	
	
	for (i=1; i <= _nb_pas;i++)
	{
		for (list<Vertex>::iterator it = profil.begin(); it != profil.end(); it++)
		{
 			xtmp = cos(i*pas_angulaire) * it->_x - sin(i*pas_angulaire) * it->_z;
			ytmp = it->_y;
			ztmp = sin(i*pas_angulaire) * it->_x + cos(i*pas_angulaire) * it->_z;
			_points.push_back(Vertex(xtmp, ytmp, ztmp, it->_r, it->_g, it->_b, it->_a));
		}
	}
}

/*********************************/
/****** Fonctions de dessin ******/
/*********************************/

void RevolutionObject::_joinPoints()
{
  float coefH = 1.0 / (_nb_vertex-1);
  float coefV = 1.0 / _nb_pas;

  // Chargement de la texture
  if (_tex)
  _tex->loadTexture();

  // Activation du mode texture
  glEnable(GL_TEXTURE_2D);
  glColor3f(1,1,1);

  for(int i=0;i<_nb_pas; i++)//ligne
  {
    for(int j=0; j<(_nb_vertex-1); j++)//colonne
    {
    
    vector<Vertex> vectPoints;
    vectPoints.insert(vectPoints.end(),_points.begin(),_points.end());
      glBegin(GL_QUADS);
        //Nord Ouest
        glColor3f(
        	vectPoints[j+i*_nb_vertex]._r,
                vectPoints[j+i*_nb_vertex]._g,
                vectPoints[j+i*_nb_vertex]._b
        );
        glTexCoord2f(i*coefV,j*coefH); //0.0
        glVertex3d(
                vectPoints[j+i*_nb_vertex]._x,
                vectPoints[j+i*_nb_vertex]._y,
                vectPoints[j+i*_nb_vertex]._z
        );
        //Nord Est
        glColor3f(
        	vectPoints[j+(i+1)*_nb_vertex]._r,
                vectPoints[j+(i+1)*_nb_vertex]._g,
                vectPoints[j+(i+1)*_nb_vertex]._b
        );
        glTexCoord2f((i+1)*coefV,j*coefH); //1.0
        glVertex3d(
                vectPoints[j+(i+1)*_nb_vertex]._x,
                vectPoints[j+(i+1)*_nb_vertex]._y,
                vectPoints[j+(i+1)*_nb_vertex]._z
        );
        //Sud Est
        glColor3f(
        	vectPoints[j+1+(i+1)*_nb_vertex]._r,
                vectPoints[j+1+(i+1)*_nb_vertex]._g,
                vectPoints[j+1+(i+1)*_nb_vertex]._b
        );
        glTexCoord2f((i+1)*coefV,(j+1)*coefH);//1.0
        glVertex3d(
                vectPoints[j+1+(i+1)*_nb_vertex]._x,
                vectPoints[j+1+(i+1)*_nb_vertex]._y,
                vectPoints[j+1+(i+1)*_nb_vertex]._z
        );
        //Sud Ouest
        glColor3f(
        	vectPoints[j+1+i*_nb_vertex]._r,
                vectPoints[j+1+i*_nb_vertex]._g,
                vectPoints[j+1+i*_nb_vertex]._b
        );
        glTexCoord2f(i*coefV,(j+1)*coefH); //0.0
        glVertex3d(
                vectPoints[j+1+i*_nb_vertex]._x,
                vectPoints[j+1+i*_nb_vertex]._y,
                vectPoints[j+1+i*_nb_vertex]._z
        );
      glEnd();
    }
  }
  // Desactivation du mode texture
  glDisable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, 0);
}

void RevolutionObject::_draw()
{
	if (_material)
		_material->display();
_joinPoints();
}
